﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Windows.Forms;
using RMS.Common;
using RMS.DB;

namespace RMS
{
    public partial class frmLooseStone : IChangeFont
    {
        private int precisionFormat = Convert.ToInt32(ConfigurationManager.AppSettings["Precision"]);
        //
        private readonly LooseStoneMng _stoneMng = new LooseStoneMng();
        private string certNoFromListItem;
        private List<string> listItemCertNo = new List<string>();
        public frmLooseStone()
        {
            InitializeComponent();
            listItemCertNo = _stoneMng.GetAllItemsId();
        }
        public frmLooseStone(string _CertNo)
        {
            InitializeComponent();
            certNoFromListItem = _CertNo;
            listItemCertNo = _stoneMng.GetAllItemsId();
        }
        private void frmLooseStone_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(certNoFromListItem))
            {
                LoadItemFromList(certNoFromListItem);
                btnAdd.Enabled = false;
                btnUpdate.Enabled = btnDel.Enabled = true;
            }
            else
            {
                btnAdd.Enabled = true;
                btnUpdate.Enabled = btnDel.Enabled = false;
            }
            txtCertNo.Focus();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int qty = string.IsNullOrEmpty(txtQuantity.Text.Trim()) ? 0 : Convert.ToInt32(txtQuantity.Text.Trim());
            decimal weight = string.IsNullOrEmpty(txtWeight.Text.Trim()) ? 0 : Convert.ToDecimal(txtWeight.Text.Trim());
            decimal importPrice = string.IsNullOrEmpty(txtImportPrice.Text.Trim())
                                      ? 0
                                      : Convert.ToDecimal(txtImportPrice.Text.Trim());
            decimal length = string.IsNullOrEmpty(txtLength.Text.Trim())
                                 ? 0
                                 : Convert.ToDecimal(txtLength.Text.Trim());
            decimal width = string.IsNullOrEmpty(txtWidth.Text.Trim())
                                ? 0
                                : Convert.ToDecimal(txtWidth.Text.Trim());
            decimal depthHeight = string.IsNullOrEmpty(txtDepthHeight.Text.Trim())
                                      ? 0
                                      : Convert.ToDecimal(txtDepthHeight.Text.Trim());
            decimal avgWidth = string.IsNullOrEmpty(txtAvgWidth.Text.Trim())
                                   ? 0
                                   : Convert.ToDecimal(txtAvgWidth.Text.Trim());
            decimal depth = string.IsNullOrEmpty(txtDepth.Text.Trim())
                                ? 0
                                : Convert.ToDecimal(txtDepth.Text.Trim());
            decimal table = string.IsNullOrEmpty(txtTable.Text.Trim())
                                ? 0
                                : Convert.ToDecimal(txtTable.Text.Trim());


            if (_stoneMng.AddNewItem(txtCertNo.Text.Trim(), txtDesc.Text.Trim(), qty, importPrice,
                                     txtStoneType.Text.Trim(), txtCertType.Text.Trim(), dtpCertDate.Value,
                                     txtShape.Text.Trim(), txtColor.Text.Trim(), txtClarity.Text.Trim(),
                                     txtCut.Text.Trim(), length, width, depthHeight, avgWidth, depth, weight, table,txtCategory.Text.Trim()))
            {
                MessageBox.Show(Constants.Msg.AddNewSuccessful);
                ClearAllField();
                listItemCertNo = _stoneMng.GetAllItemsId();
            }
        }

        private void ClearAllField()
        {
            txtAvgWidth.Text =
                txtCertNo.Text =
                txtCertType.Text =
                txtClarity.Text =
                txtColor.Text =
                txtCut.Text =
                txtDepth.Text =
                txtDepthHeight.Text =
                txtDesc.Text =
                txtImportPrice.Text =
                txtLength.Text =
                txtShape.Text =
                txtStoneType.Text =
                txtTable.Text =
                txtWeight.Text =
                txtCategory.Text =
                txtWidth.Text = "";
                txtQuantity.Text = "1";
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int qty = string.IsNullOrEmpty(txtQuantity.Text.Trim()) ? 0 : Convert.ToInt32(txtQuantity.Text.Trim());
            decimal weight = string.IsNullOrEmpty(txtWeight.Text.Trim()) ? 0 : Convert.ToDecimal(txtWeight.Text.Trim());
            decimal importPrice = string.IsNullOrEmpty(txtImportPrice.Text.Trim())
                                      ? 0
                                      : Convert.ToDecimal(txtImportPrice.Text.Trim());
            decimal length = string.IsNullOrEmpty(txtLength.Text.Trim())
                                 ? 0
                                 : Convert.ToDecimal(txtLength.Text.Trim());
            decimal width = string.IsNullOrEmpty(txtWidth.Text.Trim())
                                ? 0
                                : Convert.ToDecimal(txtWidth.Text.Trim());
            decimal depthHeight = string.IsNullOrEmpty(txtDepthHeight.Text.Trim())
                                      ? 0
                                      : Convert.ToDecimal(txtDepthHeight.Text.Trim());
            decimal avgWidth = string.IsNullOrEmpty(txtAvgWidth.Text.Trim())
                                   ? 0
                                   : Convert.ToDecimal(txtAvgWidth.Text.Trim());
            decimal depth = string.IsNullOrEmpty(txtDepth.Text.Trim())
                                ? 0
                                : Convert.ToDecimal(txtDepth.Text.Trim());
            decimal table = string.IsNullOrEmpty(txtTable.Text.Trim())
                                ? 0
                                : Convert.ToDecimal(txtTable.Text.Trim());


            if (_stoneMng.UpdateItem(txtCertNo.Text.Trim(), txtDesc.Text.Trim(), qty, importPrice,
                                     txtStoneType.Text.Trim(), txtCertType.Text.Trim(), dtpCertDate.Value,
                                     txtShape.Text.Trim(), txtColor.Text.Trim(), txtClarity.Text.Trim(),
                                     txtCut.Text.Trim(), length, width, depthHeight, avgWidth, depth, weight, table, txtCategory.Text.Trim()))
            {
                MessageBox.Show(Constants.Msg.UpdateSuccessful);                
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Msg.DeleteItem, "Chú ý", MessageBoxButtons.YesNo) != DialogResult.Yes) return;
            if (_stoneMng.DeleteItem(txtCertNo.Text.Trim()))
            {
                MessageBox.Show(Constants.Msg.DeleteSuccessful);
                ClearAllField();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            var frm = new frmListStone() { MdiParent = frmMain.ActiveForm };
            frm.Show();
            this.Close();     
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadItemFromList(txtCertNo.Text.Trim());
        }
        private void LoadItemFromList(string id)
        {
            var item = _stoneMng.GetItemByElements(id);
            if (item == null)
            {               
                return;
            }
            txtAvgWidth.Text = Convert.ToString(item.AvgWidth);
            txtCertNo.Text = item.CertNo;
            txtCertType.Text = item.CertType;
            txtClarity.Text = item.Clarity;
            txtColor.Text = item.Color;
            txtCut.Text = item.Cut;
            txtDepth.Text = Convert.ToString(item.Depth);
            txtDepthHeight.Text = Convert.ToString(item.DepthHeight);
            txtDesc.Text = item.Description;
            txtImportPrice.Text = Utilities.FormatNumber(Convert.ToDecimal(item.ImportPrice), precisionFormat);
            txtLength.Text = Convert.ToString(item.Length);
            txtShape.Text = item.Shape;
            txtStoneType.Text = item.StoneType;
            txtTable.Text = Convert.ToString(item.Table);
            txtWeight.Text = Utilities.FormatNumber(Convert.ToDecimal(item.Weight), 3);
            txtWidth.Text = Convert.ToString(item.Width);
            txtQuantity.Text = Convert.ToString(item.Quantity);
            txtCategory.Text = item.Category;
        }

        private void txtImportPrice_Leave(object sender, EventArgs e)
        {
            try
            {
                var a = Convert.ToDecimal(txtImportPrice.Text.Trim());

            }catch(FormatException)
            {
                txtImportPrice.Text = "";
            }
                                
            if (!string.IsNullOrEmpty(txtImportPrice.Text.Trim()))
            {
                txtImportPrice.Text = Utilities.FormatNumber(Convert.ToDecimal(txtImportPrice.Text), precisionFormat);
            }
        }

        private void txtImportPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            validateNumeric(e);
        }
        private void validateNumeric(KeyPressEventArgs e)
        {
            var key = e.KeyChar;
            if ((key >= 48) && (key <= 58) || (key == 46) || key == 8 || key == 9 || (key >= 37 && key <= 40) || key == 144 || key == ',')
            {
                e.Handled = false;
            }
            else e.Handled = true;
        }

        private void txtLength_KeyPress(object sender, KeyPressEventArgs e)
        {
            validateNumeric(e);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var location = new DialogStoneProperties(Constants.StoneType)
            {
                ShowInTaskbar = false
            };
            if (location.ShowDialog() == DialogResult.OK)
            {
                txtStoneType.Text = location.result;
            }
        }

        private void btnDlgShape_Click(object sender, EventArgs e)
        {
            var location = new DialogStoneProperties(Constants.ShapeType)
            {
                ShowInTaskbar = false
            };
            if (location.ShowDialog() == DialogResult.OK)
            {
                txtShape.Text = location.result;
            }
        }

        private void btnDlgCut_Click(object sender, EventArgs e)
        {
            var location = new DialogStoneProperties(Constants.CutType)
            {
                ShowInTaskbar = false
            };
            if (location.ShowDialog() == DialogResult.OK)
            {
                txtCut.Text = location.result;
            }
        }

        private void btnDlgCertType_Click(object sender, EventArgs e)
        {
            var location = new DialogStoneProperties(Constants.CertType)
            {
                ShowInTaskbar = false
            };
            if (location.ShowDialog() == DialogResult.OK)
            {
                txtCertType.Text = location.result;
            }
        }

        private void btnDlgColor_Click(object sender, EventArgs e)
        {
            var location = new DialogStoneProperties(Constants.ColorType)
            {
                ShowInTaskbar = false
            };
            if (location.ShowDialog() == DialogResult.OK)
            {
                txtColor.Text = location.result;
            }
        }

        private void btnDlgClarity_Click(object sender, EventArgs e)
        {
            var location = new DialogStoneProperties(Constants.ClarityType)
            {
                ShowInTaskbar = false
            };
            if (location.ShowDialog() == DialogResult.OK)
            {
                txtClarity.Text = location.result;
            }
        }

        private void txtCertNo_Leave(object sender, EventArgs e)
        {
            var currentCertNo = txtCertNo.Text.Trim();
            bool flag = false;
            if(listItemCertNo == null)
            {
                btnAdd.Enabled = true;
                btnUpdate.Enabled = btnDel.Enabled = false;
                return;
            }
            if (!string.IsNullOrEmpty(currentCertNo))
            {
                foreach (string _id in listItemCertNo)
                {
                    if (currentCertNo == _id)
                    {
                        flag = true;
                        break;
                    }
                }
                if (flag)
                {
                    btnAdd.Enabled = false;
                    btnUpdate.Enabled = btnDel.Enabled = true;
                }
                else
                {
                    btnAdd.Enabled = true;
                    btnUpdate.Enabled = btnDel.Enabled = false;
                }
            }
            else
            {
                btnAdd.Enabled = true;
                btnUpdate.Enabled = btnDel.Enabled = false;
            }
            

        }
    }
}